package com.base.netlib.transform

import com.base.netlib.constant.ResultCode
import com.base.netlib.exception.BusinessException
import com.base.netlib.javabean.BaseData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map

fun <T : BaseData<R>, R> Flow<T>.transformData(): Flow<R?> {
    return map { value ->
        if (ResultCode.SUCCESS == value.code) {
            return@map value.data
        }
        throw BusinessException(value.code, value.description)
    }.flowOn(Dispatchers.IO)
}


